package com.util;

import android.util.Log;

import com.entity.Authority;

import org.chromium.net.NetworkException;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class UploadHelper {
    public static final String TAG = "UploadHelper";
    private final OkHttpClient client = new OkHttpClient();

    public  String upload(byte[] bytes) {

        RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), bytes);

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", "android.png", fileBody)
                .build();

        Request request = new Request.Builder()
                .url(HttpUtils.Api+"File/Uploading")
                .header("Authorization", Authority.getAuthorization())
                .post(requestBody)
                .build();

        Response response;
        try {
            response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            Log.d(TAG,"upload IOException ",e);
        }
        return null;
    }
}
